import 'dart:async';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:lcapp/api/interface/user_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/xian_huo_data_entity.dart';
import 'package:lcapp/model/event/event_bus.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/page/min/min_xh_change_price.dart';
import 'package:lcapp/page/min/min_xianhuo_item.dart';
import 'package:lcapp/page/search/xianhuo_item.dart';

class MinXianHuoPage extends StatefulWidget {
  final bool isMine;
  final int uid;
  const MinXianHuoPage({Key? key, required this.isMine, required this.uid}) : super(key: key);

  @override
  _MinXianHuoPageState createState() => _MinXianHuoPageState();
}

class _MinXianHuoPageState extends State<MinXianHuoPage> {
  int pageNum = 1;
  int pageSize  =10;
  List<XianHuoDataEntity> xianHuos = [];
  late StreamSubscription _streamSubscription;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _streamSubscription = eventBus.on<FabuState>().listen((event) {
      if (event.type == 0) {
        pageNum=1;
        getXianHuo();
      }
    });
    getXianHuo();
  }

  getXianHuo()async{
    ResponseData res = await UserAPI.getMinXianHuos(widget.uid, pageNum, pageSize);
    if(res.code==200){
      if(pageNum==1){
        xianHuos.clear();
      }
      List arr = res.data??[];
      for (var element in arr) {
        XianHuoDataEntity entity = XianHuoDataEntity().fromJson(element);
        xianHuos.add(entity);
      }
    }
    if(mounted){setState(() {
    });}
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _streamSubscription.cancel();
  }

  @override
  Widget build(BuildContext context) {

    return Container(
      margin: EdgeInsets.only(top: 20.w,left: 20.w,right: 20.w),
      child: EasyRefresh(
        header: myRefreshHeader(),
        footer: myRefreshFooter(),
        onRefresh: ()async{
          pageNum=1;
          getXianHuo();
        },
        onLoad: ()async{
          pageNum++;
          getXianHuo();
        },
        child: widget.isMine?
        ListView.builder(
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            itemCount: xianHuos.length,
            itemBuilder: (BuildContext context, int i) {
              return MinXianHuoItm(entity: xianHuos[i],editClick: (index)async{
                if(index==0){
                  ResponseData res = await UserAPI.deleteMinXianHuos(xianHuos[i].id!);
                  if(res.code==200){
                    setState(() {
                      xianHuos.removeAt(i);
                    });
                  }
                }else if(index==1){
                  Navigator.of(context).pushNamed('FaBuSoldPage',arguments: {
                    'fabuType':xianHuos[i].publishType,
                    'id':xianHuos[i].id
                  }).then((value){
                    pageNum=1;
                    getXianHuo();
                  });
                }else if(index==2){
                  showModalBottomSheet(
                      context: context,
                      backgroundColor: Colors.transparent,
                      builder: (context){
                        return MinXHChangePrice(arguments: {
                          'entity':xianHuos[i],
                          'callback':(){
                            setState(() {

                            });
                          }
                        },);
                      });
                }
              },);
            }) :
        AlignedGridView.count(
          padding: EdgeInsets.zero,
          itemCount: xianHuos.length,
          crossAxisCount: 2,
          crossAxisSpacing: 10.w,
          mainAxisSpacing: 10.w,
          itemBuilder: (context,index){
            return XianHuoItem(entity: xianHuos[index], chooseFun: (){},);
          },
        ),
      ),
    );
  }
}
